package com.moshang.blog.controller.back.monitor;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.moshang.blog.config.shiro.ShiroUtil;
import com.moshang.blog.core.annotation.SystemLog;
import com.moshang.blog.core.enums.OnlineStatus;
import com.moshang.blog.core.utils.PageUtil;
import com.moshang.blog.core.utils.RestResponse;
import com.moshang.blog.core.utils.StringUtils;
import com.moshang.blog.entity.SysUserOnline;
import com.moshang.blog.service.SysUserOnlineService;
import com.moshang.blog.shiro.session.OnlineSession;
import com.moshang.blog.shiro.session.OnlineSessionDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.WebUtils;

import javax.servlet.ServletRequest;
import java.util.Collections;
import java.util.Map;

/**
 * @program: blog
 * @description: 在线用户控制器类
 * @author: moshang
 * @create: 2019-01-29 13:41
 **/
@Controller
@RequestMapping("/back/monitor/online")
public class SysUserOnlineController {

    @Autowired
    private SysUserOnlineService sysUserOnlineService;
    @Autowired
    private OnlineSessionDAO onlineSessionDAO;
    private String prefix = "back/monitor/online";


    @GetMapping("list")
    @SystemLog("跳转在线用户列表页面")
    public String list(){
        return prefix+"/list";
    }

    @GetMapping("findList")
    @ResponseBody
    public PageUtil<SysUserOnline> list(@RequestParam(value = "page",defaultValue = "1")Integer page,
                                        @RequestParam(value = "limit",defaultValue = "10")Integer limit, ServletRequest request){
        Map<String,Object> map = WebUtils.getParametersStartingWith(request, "s_");
        PageUtil<SysUserOnline> pageUtils = new PageUtil<>();
        QueryWrapper<SysUserOnline> wrapper = new QueryWrapper<SysUserOnline>();
        sysUserOnlineService.save();
        if(!map.isEmpty()){
            String loginName = (String) map.get("loginName");
            String loginLocation = (String) map.get("loginLocation");
            if(StringUtils.isNotBlank(loginName)) {
                wrapper.eq("login_name", loginName);
            }
            if(StringUtils.isNotBlank(loginLocation)) {
                wrapper.like("login_location", loginLocation);
            }
        }
        wrapper.orderByDesc("last_access_time");
        IPage<SysUserOnline> tablePage = sysUserOnlineService.page(new Page<>(page,limit), wrapper);
        pageUtils.setCount((int) tablePage.getTotal());
        pageUtils.setData(tablePage.getRecords());
        return pageUtils;
    }

   // @RequiresPermissions("monitor:online:batchForceLogout")
    @SystemLog( "批量在线用户下线")
    @PostMapping("/batchForceLogout")
    @ResponseBody
    public RestResponse batchForceLogout(@RequestParam("sessionIds[]") String[] ids)
    {
        for (String sessionId : ids)
        {
            SysUserOnline online = sysUserOnlineService.selectOnlineById(sessionId);
            if (online == null)
            {
                return  RestResponse.failure("用户已下线");
            }
            OnlineSession onlineSession = (OnlineSession) onlineSessionDAO.readSession(online.getSessionId());
            if (onlineSession == null)
            {
                return RestResponse.failure("用户已下线");
            }
            if (sessionId.equals(ShiroUtil.getSessionId()))
            {
                return RestResponse.failure("当前登陆用户无法强退");
            }
            onlineSession.setStatus(OnlineStatus.off_line);
            online.setStatus(OnlineStatus.off_line);
            sysUserOnlineService.saveOnline(online);
        }
        return RestResponse.success();
    }

//    @RequiresPermissions("monitor:online:forceLogout")
    @SystemLog("在线用户下线")
    @PostMapping("/forceLogout")
    @ResponseBody
    public RestResponse forceLogout(String sessionId)
    {
        SysUserOnline online = sysUserOnlineService.selectOnlineById(sessionId);
        if (sessionId.equals(ShiroUtil.getSessionId()))
        {
            return RestResponse.failure("当前登陆用户无法强退");
        }
        if (online == null)
        {
            return RestResponse.failure("用户已下线");
        }
        OnlineSession onlineSession = (OnlineSession) onlineSessionDAO.readSession(online.getSessionId());
        if (onlineSession == null)
        {
            return RestResponse.failure("用户已下线");
        }
        onlineSession.setStatus(OnlineStatus.off_line);
        online.setStatus(OnlineStatus.off_line);
        sysUserOnlineService.saveOnline(online);
        return RestResponse.success();
    }
}
